package com.ycrlkj.android.modulespublic.common.utils.xpopup;

import android.app.Activity;

import com.lxj.xpopup.XPopup;
import com.lxj.xpopup.impl.LoadingPopupView;
import com.lxj.xpopup.interfaces.OnCancelListener;
import com.lxj.xpopup.interfaces.OnConfirmListener;
import com.lxj.xpopup.interfaces.OnSelectListener;
import com.ycrlkj.android.modulesbase.libbase.utils.Logger;


public class XPopupUtil {

    public static LoadingPopupView showLoading(Activity activity) {
        LoadingPopupView loadingPopup = (LoadingPopupView) new XPopup.Builder(activity)
                .dismissOnBackPressed(false)
                .isLightNavigationBar(true)
                .asLoading("加载中")
                .show();
        return loadingPopup;
    }

    public static LoadingPopupView showLoading(Activity activity, String title) {
        LoadingPopupView loadingPopup = (LoadingPopupView) new XPopup.Builder(activity)
                .dismissOnBackPressed(false)
                .isLightNavigationBar(true)
                //.asLoading("加载中")
                .asLoading(title)
                .show();
        return loadingPopup;
    }

    public static void hideLoading(LoadingPopupView loadingPopup) {
        loadingPopup.dismiss();
    }

    public static void hideLoadingDelay(LoadingPopupView loadingPopup) {
        loadingPopup.delayDismissWith(5000, null);
    }

    public static void hideLoadingDelay(LoadingPopupView loadingPopup, int time) {
        loadingPopup.delayDismissWith(time, null);
    }

    public static void hideLoadingDelay(LoadingPopupView loadingPopup, int time, Runnable runnable) {
        loadingPopup.delayDismissWith(time, runnable);
    }

    public static void loadingChangeTitleDelay(LoadingPopupView loadingPopup, int time, String title) {
        loadingPopup.postDelayed(new Runnable() {
            @Override
            public void run() {
                loadingPopup.setTitle(title);
            }
        }, time);
    }

    /**
     * XPopup第三方包弹出对话框:
     *
     * @param title           –对话框标题，传空串会隐藏标题
     * @param content         –对话框内容
     * @param *cancelBtnText   –取消按钮的文字内容
     * @param *confirmBtnText  –确认按钮的文字内容
     * @param *confirmListener –点击确认的监听器
     * @param *cancelListener  –点击取消的监听器
     * @param *isHideCancel    –是否隐藏取消按钮
     * @param *bindLayoutId    –自定义的布局Id，没有则传0；要求自定义布局中必须包含的TextView以及id有：tv_title，tv_content，tv_cancel，tv_confirm
     */
    public static void showAlert(Activity activity, String title, String content) {
        new XPopup.Builder(activity)
                .dismissOnTouchOutside(false) //点击空白是否关闭弹窗, 默认为 true
                .dismissOnBackPressed(true) //按返回键是否关闭弹窗, 默认为 true
                .hasStatusBarShadow(true) //启用状态栏阴影
                .asConfirm(
                        title,
                        content,
                        "取消",
                        "确定",
                        null,
                        null,
                        true,
                        0)
                .show();
    }

    public static void showAlert(Activity activity, String content) {
        new XPopup.Builder(activity)
                .dismissOnTouchOutside(false) //点击空白是否关闭弹窗, 默认为 true
                .dismissOnBackPressed(true) //按返回键是否关闭弹窗, 默认为 true
                .hasStatusBarShadow(true) //启用状态栏阴影
                .asConfirm(
                        "",
                        content,
                        "取消",
                        "确定",
                        null,
                        null,
                        true,
                        0)
                .show();
    }

    public static void showAlert(Activity activity, String content, OnConfirmListener listener) {
        new XPopup.Builder(activity)
                .dismissOnTouchOutside(false) //点击空白是否关闭弹窗, 默认为 true
                .dismissOnBackPressed(true) //按返回键是否关闭弹窗, 默认为 true
                .hasStatusBarShadow(true) //启用状态栏阴影
                .asConfirm(
                        "",
                        content,
                        "取消",
                        "确定",
                        listener,
                        null,
                        true,
                        0)
                .show();
    }

    public static void showModal(Activity activity, String title, String content, OnConfirmListener confirmCallBack, OnCancelListener cancelCallBack) {
        new XPopup.Builder(activity)
                .dismissOnTouchOutside(false) //点击空白是否关闭弹窗, 默认为 true
                .dismissOnBackPressed(true) //按返回键是否关闭弹窗, 默认为 true
                .hasStatusBarShadow(true) //启用状态栏阴影
                .asConfirm(
                        title,
                        content,
                        "取消",
                        "确定",
                        confirmCallBack,
                        cancelCallBack,
                        false,
                        0)
                .show();
    }

    public static void showModal(Activity activity, String title, String content, OnConfirmListener confirmCallBack) {
        new XPopup.Builder(activity)
                .dismissOnTouchOutside(false) //点击空白是否关闭弹窗, 默认为 true
                .dismissOnBackPressed(true) //按返回键是否关闭弹窗, 默认为 true
                .hasStatusBarShadow(true) //启用状态栏阴影
                .asConfirm(
                        title,
                        content,
                        "取消",
                        "确定",
                        confirmCallBack,
                        null,
                        false,
                        0)
                .show();
    }

    public static void showModal(Activity activity, String content, OnConfirmListener confirmCallBack, OnCancelListener cancelCallBack) {
        new XPopup.Builder(activity)
                .dismissOnTouchOutside(false) //点击空白是否关闭弹窗, 默认为 true
                .dismissOnBackPressed(true) //按返回键是否关闭弹窗, 默认为 true
                .hasStatusBarShadow(true) //启用状态栏阴影
                .asConfirm(
                        "",
                        content,
                        "取消",
                        "确定",
                        confirmCallBack,
                        cancelCallBack,
                        false,
                        0)
                .show();
    }

    public static void showModal(Activity activity, String content, OnConfirmListener confirmCallBack) {
        new XPopup.Builder(activity)
                .dismissOnTouchOutside(false) //点击空白是否关闭弹窗, 默认为 true
                .dismissOnBackPressed(true) //按返回键是否关闭弹窗, 默认为 true
                .hasStatusBarShadow(true) //启用状态栏阴影
                .asConfirm(
                        "",
                        content,
                        "取消",
                        "确定",
                        confirmCallBack,
                        null,
                        false,
                        0)
                .show();
    }

    public static void showModal(Activity activity, String content, boolean onBackPressed, OnConfirmListener confirmCallBack) {
        new XPopup.Builder(activity)
                .dismissOnTouchOutside(false) //点击空白是否关闭弹窗, 默认为 true
                .dismissOnBackPressed(onBackPressed) //按返回键是否关闭弹窗, 默认为 true
                .hasStatusBarShadow(true) //启用状态栏阴影
                .asConfirm(
                        "",
                        content,
                        "取消",
                        "确定",
                        confirmCallBack,
                        null,
                        false,
                        0)
                .show();
    }

    public static void showSelect(Activity activity, String title, String[] arr, int defaultChecked, OnSelectListener onSelectListener) {
        new XPopup.Builder(activity)
                .isDestroyOnDismiss(true) //对于只使用一次的弹窗，推荐设置这个
                .asCenterList(title, arr, null, defaultChecked, onSelectListener)
                .show();
    }

    public static void showSelect(Activity activity, String title, String[] arr, int defaultChecked, OnSelectListener onSelectListener, int maxHeight) {
        new XPopup.Builder(activity)
                .isDestroyOnDismiss(true) //对于只使用一次的弹窗，推荐设置这个
                .maxHeight(maxHeight)
                .asCenterList(title, arr, null, defaultChecked, onSelectListener)
                .show();
    }

}